﻿using System;
using System.Collections.Generic;
using System.Text;
using MbUnit.Framework;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System.IO;
using System.Reflection;
using System.Xml;
using Castle.MonoRail.TestSupport;
using MyMessenger.Core;
using MyMessenger.Core.Services.Impl;

namespace MyMessenger.Tests
{
    public abstract class MessengerTestFixtureBase : BaseControllerTest
    {
        protected WindsorContainer container;
        protected SessionScope scope;


        protected User Olo
        {
            get { return new UserService().Find("olo"); }
        }

        protected User Bolo
        {
            get { return new UserService().Find("bolo"); }
        }

        [TestFixtureSetUp]
        public virtual void TestFixtureSetUp()
        {
            if (container == null)
                container = new WindsorContainer(new XmlInterpreter());

            scope = new SessionScope();
            CreatreSchema();
        }

        public void ResetDb()
        {
            scope.Dispose();
            TestFixtureSetUp();
        }

        private static void CreatreSchema()
        {
            ISessionFactoryHolder sessionFactoryHolder = ActiveRecordMediator.GetSessionFactoryHolder();
            Configuration configuration = sessionFactoryHolder.GetConfiguration(typeof(ActiveRecordBase));
            ISession session = sessionFactoryHolder.CreateSession(typeof(ActiveRecordBase));
            new SchemaExport(configuration).Execute(false, true, false, true, session.Connection, null);
        }

        [TestFixtureTearDown]
        public virtual void TestFixtureTearDown()
        {
            scope.Dispose();
        }


        public ISession Session
        {
            get
            {
                return ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(ActiveRecordBase));
            }
        }

        public void CreateExampleData()
        {
            AddUser("admin", "admin");
            AddUser("olo", "123");
            AddUser("bolo", "123");
            Session.Flush();
        }

        protected void AddUser(string name, string password)
        {
            User u = new User();
            u.Name = name;
            u.Password = password;
            new UserService().Save(u);
        }

        protected Messege AddMessage(string msg, User fromUser, User toUser)
        {
            Messege m = new Messege();
            m.Text = msg;
            m.FromUser = fromUser;
            m.ToUser = toUser;
            ActiveRecordMediator<Messege>.SaveAndFlush(m);
            return m;
        }


        protected void DeleteAllMessages()
        {
            ActiveRecordMediator<Messege>.DeleteAll();
        }
    }
}
